package xyz.test;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import java.util.Date;

import xyz.dao.NoteDatabase;
import xyz.entity.Note;
import xyz.viewmodel.NoteViewModel;

public class EditNoteActivity extends AppCompatActivity {

    public static final int STATE_NEW = 1;
    public static final int STATE_EDIT = 2;

    private int state;

    private Note mNote;

    private NoteViewModel mVM;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setContentView(R.layout.activity_edit_note);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.cs, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        Log.d("TAG", "onOptionsItemSelected: " + R.id.home + " " + item.getItemId());
        switch (item.getItemId()) {
            case android.R.id.home: {
                Intent i = new Intent(EditNoteActivity.this, MainActivity.class);

                EditNoteActivity.this.startActivity(i);
                return true;
            }
            case R.id.save: {
                EditText $title = findViewById(R.id.edit_title);
                EditText $text = findViewById(R.id.edit_text);
                EditNoteActivity.this.saveNote($title.getText().toString(), $text.getText().toString());
                Log.d("EditNoteActivity", "onOptionsItemSelected: save");
                return true;
            }
        }

        return super.onOptionsItemSelected(item);
    }

    private boolean mChange = false;

    @Override
    protected void onStart() {
        super.onStart();

        mVM = new ViewModelProvider(this).get(NoteViewModel.class);


        final EditText $title = findViewById(R.id.edit_title);
        final EditText $text = findViewById(R.id.edit_text);


        Intent i = getIntent();

        state = i.getIntExtra("state", 0);


        int id = getIntent().getIntExtra("id", 1);
        LiveData<Note> note = this.getLiveNote(id);
        note.observe(this, new Observer<Note>() {
            @Override
            public void onChanged(Note note) {
                EditNoteActivity.this.mNote = note;
                if (note != null) {

                    String title = "";
                    String text = "";
                    title = note.title;
                    text = note.content;


                    try {

                        $title.setText(title);
                        $text.setText(text);
                        mChange = true;
                    } catch (Exception e) {

                    }
                }
            }
        });


        $title.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
//                if (mChange)
//                    $title.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
            }
        });

    }


    protected LiveData<Note> getLiveNote(int id) {
        return mVM.getOneNoteLiveData(id);
    }

    private void saveNote(String title, String text) {

        NoteDatabase noteDb = NoteDatabase.getInstance(EditNoteActivity.this);

        if (state == STATE_NEW) {
            Note note = new Note();
            note.setText(text);
            note.setTitle(title);
            note.created = new Date();
            mVM.addNote(note);
        } else {
            mNote.setText(text);
            mNote.setTitle(title);
            mNote.updated = new Date();
            mVM.updateNote(mNote, new OnUpdatedListener() {

                @Override
                public void OnUpdated() {
                    mChange = false;
                }
            });
        }

    }

    void OnUpdated() {

    }

    public static interface OnUpdatedListener {
        void OnUpdated();
    }
}